package com.loklok.code.confuse.ui;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.loklok.code.confuse.dto.GenerateCodeDto;
import com.loklok.code.confuse.extension.FeignInterfaceInfoCache;
import com.loklok.code.confuse.extension.PathConfigSettingCache;
import com.loklok.code.confuse.model.ClassMetaData;
import com.loklok.code.confuse.model.CodePathConfig;
import com.loklok.code.confuse.model.FeignInterfaceInfo;
import com.loklok.code.confuse.service.FullUpdateTemplateService;
import com.loklok.code.confuse.service.UrlGenerateCodeService;
import com.loklok.code.confuse.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public class UrlGenerateCodeVmDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextArea urlArea;
    private JTextField packageTField;
    private JCheckBox needControllerBox;
    private JButton buttonCancel;

    public UrlGenerateCodeVmDialog(AnActionEvent anActionEvent) {
        setContentPane(contentPane);
        setModal(true);
        setResizable(true);
        setSize(800, 500);
        setLocation(600, 200);
        getRootPane().setDefaultButton(buttonOK);

        Project project = anActionEvent.getProject();
        PathConfigSettingCache state = PathConfigSettingCache.getInstance(project);
        if (state == null || state.getConfig() == null || state.getConfig().requiredFieldsEmpty()) {
            CommonUtil.configureMessageReminders(project);
            return;

        }
        CodePathConfig config = state.getConfig();

        buttonOK.addActionListener(e -> {
            buttonOK.setEnabled(false);
            String methodPath = urlArea.getText();
            String pckName = packageTField.getText();
            if (StringUtils.isEmpty(methodPath) || StringUtils.isEmpty(pckName)) {
                buttonOK.setEnabled(true);
                Messages.showMessageDialog("接口url或者包名不能为空!", "提示", Messages.getWarningIcon());
                return;
            }

            FeignInterfaceInfoCache instance = FeignInterfaceInfoCache.getInstance(project);
            Map<String, FeignInterfaceInfo> feignInterfaceInfoMap = instance.getFeignInterfaceInfoMap();
            if(MapUtils.isEmpty(feignInterfaceInfoMap)){
                Messages.showErrorDialog("请使用插件初始化模板 ===>【init template】", "错误");
                return;
            }

            if (DumbService.isDumb(project)) {
                DumbService.getInstance(project).showDumbModeNotification("请等待索引加载完成后再执行操作");
                return;
            }

            Set<String> notMatch = new HashSet<>();
            List<FeignInterfaceInfo> all = new ArrayList<>();

            UrlGenerateCodeService.fillFeignInterfaceInfos(methodPath, feignInterfaceInfoMap, notMatch, all);

            if(CollectionUtils.isEmpty(all)){
                Messages.showInfoMessage("输入的地址未在 Feign 中配置", "信息");
                return;
            }

            UrlGenerateCodeService.doPreGenerateCode(anActionEvent, config, pckName, all, needControllerBox.isSelected());

            if(CollectionUtils.isNotEmpty(notMatch)){
                String message = "未匹配的接口地址: " + String.join(",", notMatch);
                Messages.showInfoMessage(message, "信息");
            }
            onOK();
        });
        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
    }


    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

}
